package gr.aueb.cs.mscis.chordroid;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import gr.aueb.cs.mscis.R;
import gr.aueb.cs.mscis.chordroid.util.Sha1;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.EditText;
import android.widget.TextView;

public class FileAppActivity extends Activity {

	protected InitTask updateField;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fileapplayout);

		Button uploadButton = (Button) findViewById(R.id.button1);
		uploadButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				String filename = ((EditText) findViewById(R.id.editText1)).getText().toString();
				Log.v("Act", "hash: " + Sha1.getHash(filename));
				byte[] data = null;
				try {
					File f = new File(getFilesDir() + "/" + filename);

					if (!f.exists()) {
						Log.v("fileAppActivity", "file: " + filename
								+ " doesn't exist");
						return;
					}

					data = new byte[(int) f.length()];
					FileInputStream fis = new FileInputStream(f);
					fis.read(data);

				} catch (IOException e) {
					e.printStackTrace();
				}
				final String name = filename;
				final byte[] out = data;
				Log.v("fileAppActivity", "begin uploading: " + filename);
				Thread t1 = new Thread(new Runnable() {
					public void run() {
						ChordroidService.getApp().uploadFile(name, out);
					}
				});
				t1.start();

				updateField = new InitTask();
				updateField.execute(this);

			}
		});
		
		Button searchButton = (Button) findViewById(R.id.button2);
		searchButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {

//				Thread t1 = new Thread(new Runnable() {
//					public void run() {
				try {
					String filename = ((EditText) findViewById(R.id.editText2)).getText().toString();
					Log.v("Act", "hash2: " + Sha1.getHash(filename));
					byte[] data = null;
					data = ChordroidService.getApp().searchFile(filename);
					
					if (data != null) {
					 FileOutputStream fos =
					 openFileOutput(filename+"output",Context.MODE_WORLD_READABLE);
					 fos.write(data);
					 fos.close();
					 Log.v("fileAppActivity","file saved" );
					}

				} catch (IOException e) {
					e.printStackTrace();
				}
//					}});
//				t1.start();				

//				updateField = new InitTask();
//				updateField.execute(this);
			}
		});
	}

	protected class InitTask extends AsyncTask<View.OnClickListener, String, String> {
		// -- run intensive processes here
		// -- notice that the datatype of the first param in the class
		// definition matches the param passed to this method
		// -- and that the datatype of the last param in the class definition
		// matches the return type of this mehtod
		@Override
		protected String doInBackground(View.OnClickListener... params) {
			// -- on every iteration
			// -- runs a while loop that causes the thread to sleep for 50
			// milliseconds
			// -- publishes the progress - calls the onProgressUpdate handler
			// defined below
			// -- and increments the counter variable i by one
			while (ChordroidService.getApp().getUploadProgress() < 100.0f
					&& ChordroidService.getApp().getSeeder() != null) {
				try {
					Thread.sleep(50);
					publishProgress("uploading: "
							+ ChordroidService.getApp().getUploadProgress()
							+ " % ");
				} catch (Exception e) {
				}
			}
			return "COMPLETE!";
		}

		// -- gets called just before thread begins
		@Override
		protected void onPreExecute() {
			Log.i("makemachine", "onPreExecute()");
			super.onPreExecute();

		}

		// -- called from the publish progress
		// -- notice that the datatype of the second param gets passed to this
		// method
		@Override
		protected void onProgressUpdate(String... values) {
			super.onProgressUpdate(values);
			((TextView) findViewById(R.id.textView2)).setText((values[0]));

		}

		// -- called if the cancel button is pressed
		@Override
		protected void onCancelled() {
			super.onCancelled();
			Log.i("makemachine", "onCancelled()");
		}

		// -- called as soon as doInBackground method completes
		// -- notice that the third param gets passed to this method
		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			Log.i("makemachine", "onPostExecute(): " + result);

		}
	}

}
